<?php
//活动展示相关业务

namespace App\Services\Wx;

use App\Services\ActivityService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

class ActivityShowService
{

    /**
     * 活动列表
     * @param $page
     * @param $pageSize
     * @return array
     */
    public static function getList($page = 1, $pageSize = 20)
    {
        $pageRes = ActivityService::getPageList([
            'status' => 2,
        ], $page, $pageSize);

        return [
            'total'     => $pageRes['total'],
            'totalPage' => $pageRes['totalPage'],
            'pageSize'  => $pageRes['pageSize'],
            'list'      => self::formatList($pageRes['list']),
        ];
    }

    /**
     * 格式化列表
     * @param $list
     * @return array
     */
    public static function formatList($list = [])
    {
        $output = [];
        if (!$list) {
            return $output;
        }
        foreach ($list as $val) {
            //预定义输出字段
            $val['receive_date_start'] = Carbon::parse($val['receive_date_start'])->toDateString();
            $val['receive_date_end'] = Carbon::parse($val['receive_date_end'])->toDateString();
            $item = [
                'id'                => $val['id'],
                'title'             => $val['title'],
                'pic'               => $val['pic'],
//                'banner_pic'        => $val['banner_pic'],
                'receive_date_text' => "{$val['receive_date_start']} 至 {$val['receive_date_end']}",
            ];
            $output[] = $item;
        }
        unset($list);

        return $output;
    }

    /**
     * 获取活动详情
     * @param $activityId
     * @param $openid
     * @return array
     */
    public static function getDetail($activityId = 0, $wxUserId = 0)
    {
        if (!$activityId) {
            return resultError('参数缺失');
        }
        //获取用户
        $userInfo = UserService::getUsreInfoById($wxUserId);
        if (!$userInfo || !$userInfo->openid) {
            return resultError('用户信息有误');
        }
        $openid = $userInfo->openid;
        //获取活动信息
        $activityInfo = ActivityService::getRow([
            'id' => $activityId,
        ]);
        if (!$activityInfo) {
            return resultError('活动信息有误');
        }
        if ($activityInfo['status'] != 2) {
            return resultError('活动已下线');
        }

        //按钮文案
        $buttonStatus = 1; //按钮状态：1.我要领取；2.查看我的券；3.即将开始；4.已结束；
        $buttonStatusText = '我要领取'; //按钮文案

        $nowDate = Carbon::now()->toDateTimeString();
        if ($nowDate < $activityInfo['receive_date_start']) {
            $buttonStatus = 3;
            $buttonStatusText = '即将开始';
        } elseif ($nowDate > $activityInfo['receive_date_end']) {
            $buttonStatus = 4;
            $buttonStatusText = '已结束';
        }

        $activityInfo['receive_date_start'] = Carbon::parse($activityInfo['receive_date_start'])->toDateString();
        $activityInfo['receive_date_end'] = Carbon::parse($activityInfo['receive_date_end'])->toDateString();

        //拼装输出
        $output = [
            'id'                 => $activityInfo['id'],
            'title'              => $activityInfo['title'],
            'desc'               => $activityInfo['desc'],
            'pic'                => $activityInfo['pic'],
            'receive_date_text'  => "{$activityInfo['receive_date_start']} 至 {$activityInfo['receive_date_end']}",
            'receive_status'     => 1, //用户可领取状态：1.可领取；2.已领取；
            'type'               => $activityInfo['type'], //活动类型：1.消费券；2.补贴；
            'button_status'      => $buttonStatus, //按钮状态：1.我要领取；2.查看我的券；3.即将开始；4.已结束；
            'button_status_text' => $buttonStatusText, //按钮状态文案；
        ];

        return resultSuccess($output);
    }

    /**
     * 领取失败展示
     * @param $activityId
     * @return string
     */
    public static function receiveFailShow($activityId) {
        $text = '';
        //获取活动领取失败文案，###为约定换行符
        $lineSym = '###';
        switch ($activityId) {
            case 1:
            case 2:
                $text = "您好，您暂时没有待领的暖冬券，本次活动仅限于{$lineSym}";
                $text .= "河北省高碑店市肖官营镇受2023年京津冀水灾影响的{$lineSym}";
                $text .= "家庭，如有疑问可咨询您所在村村委。";
                break;
            case 3:
                $text = "您好，您暂时没有待领的宝宝券，本次活动仅限于{$lineSym}";
                $text .= "甘肃省积石山县受本次积石山地震影响的2023年新生儿{$lineSym}";
                $text .= "家庭，宝宝券领取人为新生儿妈妈。{$lineSym}";
                $text .= "如有疑问可咨询您所在村村医。";
                break;
        }

        return [
            'text' => $text,
        ];
    }

}
